package com.xnew.utils;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.*;

//xls输出工具
public class SefXlsWriter<T> {

    public SefXlsWriter(Class<T> c) {
        m_itemClass = c;
    }

    //解析字段的class
    private Class<T> m_itemClass;

    //用于解析对象的辅助类
    private class XlsTitle {
        public XlsTitle(String ziduan, String name) {
            this.ziduan = ziduan;
            this.name = name;

        }

        String ziduan; //字段名
        String name; //显示标题
        Field field; //对应的类的字段
        private boolean isMap; //是不是map类型


        //初始化
        public void init(Class c) {
            //map类型的直接就获取之
            if (c.isInstance(Map.class)) {
                isMap = true;
            } else {
                isMap = false;
                try {
                    this.field = c.getDeclaredField(ziduan);
                    if(this.field!=null)
                    {
                        this.field.setAccessible(true);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


        }

       //获取对象真实的值
        public String getValue(Object o) {
            if (isMap) {
                Map map = (Map) o;
                Object o1 = map.get(ziduan);
                if (o1 == null) {
                    return "";
                }
                return o1.toString();
            } else {
                if (field == null) {
                    return "";
                }
                try {
                    Object o1 = field.get(o);

                    if (o1 == null) {
                        return "";
                    }
                    return o1.toString();
                } catch (Exception e) {

                }
               return "";
            }
        }
    }

    private ArrayList<XlsTitle> titles = new ArrayList<>();

    public void addHeaderAlias(String ziduan, String name) {
        titles.add(new XlsTitle(ziduan, name));
    }

    //真实的文稿
  private HSSFWorkbook workbook;
    //  写数据
    public void write(Collection<T> objects, boolean isclose) throws Exception {
        //初始化标题
        for (XlsTitle t : titles) {
            t.init(m_itemClass);
        }
        workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        //写入标题
        HSSFRow titlerow = sheet.createRow(0);
        for (int i = 0; i < titles.size(); i++) {
            XlsTitle xlsTitle = titles.get(i);
            HSSFCell cell = titlerow.createCell(i);
            cell.setCellValue(xlsTitle.name);
        }

        //逐行输出数据
        int rownum = 1;
        for (Object item : objects) {
            HSSFRow xlsrow = sheet.createRow(rownum);
            for (int i = 0; i < titles.size(); i++) {
                XlsTitle xlsTitle = titles.get(i);
                HSSFCell cell = xlsrow.createCell(i);
                cell.setCellValue(xlsTitle.getValue(item));
            }
            rownum++;
        }
    }

    public void setOnlyAlias(boolean b)
    {
        //目前没用就为了他们省的改代码
    }

    public void flush(OutputStream outputStream,  boolean isCloseOut ){

        try {
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
